<?php

class Orders extends MY_Controller {
	var $currentPage = 'orders';
	var $caption = 'Order';
	var $acion = '';

	
	public function index()
	{
		
	//$this->team is autoload at Controller's constructor
	  // $test = $this->team->limit(1,10);

	   $this->loadAdminPage($data = '');
	}
	
	/*
	  SELECT  COUNT(*) as total, productsetid, min(createdon)

FROM set_items where productsetid = 1

GROUP BY productsetid
	 */
	
	public function create(){
		//$data = '';
		$data['itemList'] = $this->item->getAllEntries();
	    $data['setList'] = $this->set->getAllEntries();
	  
		$this->action = 'add';
		$this->loadAdminPage($data);
	}
	
	
	public function edit($id){
		//$data = '';
		$this->action = 'edit';
		//$this->set is autoload at Controller's constructor
		$data =  $this->set->getEntryById($id);
		
	    $data['itemList'] = $this->item->getAllEntries();
	   
	    $data['setItemList'] =  $this->set_items->getAllEntriesByProductsetId($id);
		$this->loadAdminPage($data);
	}
	
	 function save(){
    	//create a reference
    	$order = &$this->orders;
    	$this->setValues($order);
    	
    	if(! isset($_REQUEST['id']) || $_REQUEST['id'] == ''){
    		$order->insert();
    		$this->edit($this->db->insert_id()); 
    	}else{
    		$order->id = $_REQUEST['id'];
    		$order->update();
    		$this->edit($_REQUEST['id']); 
    	}
    	//$this->index();

    }
	
    function setValues($order){
    	
	  	$order->orderDate = $_REQUEST['orderDate'];
		$order->deliveryDate =  $_REQUEST['deliveryDate'];
		$order->deliveryTime =  $_REQUEST['deliveryTime'];
		$order->staffCode  =  $_REQUEST['staffCode'];
		$order->teamId  =  $_REQUEST['teamId'];
		$order->customerId  =  $_REQUEST['customerId'];
		$order->price  =  $_REQUEST['price'];
		$order->tax =  $_REQUEST['tax'];
		$order->discount  =  $_REQUEST['discount'];
		$order->deliverycharge =  $_REQUEST['deliverycharge'];
		$order->totalAmount  =  $_REQUEST['totalamoun'];
		$order->memo  =  $_REQUEST['memo'];
		$order->freebies  =  $_REQUEST['freebies'];
        
       // $order->description  =  $_REQUEST['description'];
    	
    }
	
	public function delete($id){
		$this->order->delete($id);
		$this->index();
	}
	
	
	
}